﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows.Input;

namespace Con10uumSample
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class Tiroir : UserControl
    {
        DispatcherTimer m_TimerButtonActionTimer = new DispatcherTimer();
        DispatcherTimer m_UpdateLayoutTimer = new DispatcherTimer();

        public event EventHandler ShowRoom1Selected;

        public Tiroir()
        {
            InitializeComponent();

            Loaded += delegate
            {
                m_TimerButtonActionTimer.Tick += delegate
                {
                    VisualStateManager.GoToState(this, "Expanded", true);
                    m_TimerButtonActionTimer.Stop();
                    VisualStateManager.GoToState(this, "End", true);
                    m_UpdateLayoutTimer.Start();
                    
                };

                m_UpdateLayoutTimer.Interval = TimeSpan.FromMilliseconds(300);

                m_UpdateLayoutTimer.Tick += delegate
                {
                    InvalidateVisual();
                    m_UpdateLayoutTimer.Stop();
                };

                m_TimerButtonActionTimer.Interval = TimeSpan.FromMilliseconds(300);
                this.MouseEnter += delegate
                {
                    m_TimerButtonActionTimer.Start();

                    VisualStateManager.GoToState(this, "End", true);
                };

                TimerButtonGrid.IsManipulationEnabled = true;

                TimerButtonGrid.TouchDown += delegate
                {
                    m_TimerButtonActionTimer.Start();
                    VisualStateManager.GoToState(this, "End", true);
                };

                TimerButtonGrid.TouchEnter += delegate 
                {
                    m_TimerButtonActionTimer.Start();
                    VisualStateManager.GoToState(this, "End", true);
                };

                TimerButtonGrid.TouchUp += delegate
                {
                    VisualStateManager.GoToState(this, "Collapsed", true);
                    m_TimerButtonActionTimer.Stop();
                    VisualStateManager.GoToState(this, "Start", true);
                    m_UpdateLayoutTimer.Start();
                };

                TimerButtonGrid.TouchLeave += delegate
                {
                    VisualStateManager.GoToState(this, "Collapsed", true);
                    m_TimerButtonActionTimer.Stop();
                    VisualStateManager.GoToState(this, "Start", true);
                    m_UpdateLayoutTimer.Start();
                };

                ShowRoom1.TouchUp += delegate
                {
                    if (ShowRoom1Selected != null)
                    {
                        ShowRoom1Selected(this, EventArgs.Empty);
                    }
                };


                ShowRoom1.Click += delegate
                {
                    if (ShowRoom1Selected != null)
                    {
                        CollapseMenu();
                        ShowRoom1Selected(this, EventArgs.Empty);
                    }
                };
            };
        }

        private void CollapseMenu()
        {
            VisualStateManager.GoToState(this, "Collapsed", true);
            m_TimerButtonActionTimer.Stop();
            VisualStateManager.GoToState(this, "Start", true);
            m_UpdateLayoutTimer.Start();
        }
    }
}
